package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.util.AliyunOSSUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;

@RestController
@Slf4j
public class UploadController {
    //通过参数注入来方便更换endPoint和bucketName，只需改变配置文件即可
    @Value("${kunkun.endPoint}")//通过注解注入参数
    private String endPoint;
    @Value("${kunkun.bucketName}")
    private String bucketName;
    @PostMapping("/upload")
    @SneakyThrows
    public Result upload(MultipartFile file){
        /*log.info("file:{}",file);
        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(","));
        String string = UUID.randomUUID().toString();//toString()将UUID.randomUUID()结果的类型转为字符串类型。
        file.transferTo(new File("E:\\temp\\"+string+substring));*/


        String originalFilename = file.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String upload = AliyunOSSUtils.upload(endPoint, bucketName, file.getBytes(), substring);
        return Result.success(upload);
    }
}
